package lesson14;

public class Auction 
{
	Car[] carsForSale;
	Person[] participants;
	boolean isOpened;

	void open()
	{
		isOpened = true;
	}
	
	void close()
	{
		isOpened = false;
	}
	
	void processSales()
	{
		//process Sales - what does it mean ?
		//   - we look at each car
		//   - for the car we decide if anyone has wants to buy it
		//   - 
		for(int i = 0; i < carsForSale.length; i++)
		{
			for(int j = 0; j < participants.length; j++)
			{
				if(carsForSale[i].owner != null)
					break;
				Person participant = participants[j];
				if(participant.willingToBuy())
				{
					makeSale(participant, carsForSale[i]);
				}
			}
		}
		//this guy. If he can buy 2 cars, we continue untill the cars are done 
		//or he buys another car.
		//we do this for all participants
	}
	
	void makeSale(Person buyer, Car car)
	{
		if(buyer.carsCapacity>0)
		{
			car.owner = buyer;
			buyer.carsCapacity--;
			System.out.println("Car " + car.model + " sold to " + buyer.name);
		}
	}
}
